using System;
using SMS.SMPPClient.PDU.Incoming;

namespace SMS.SMPPClient.Util
{
    /// <summary>
    /// Defines an Unsuccess address, used with the response to submit_multi.
    /// The spec states that both SME addresses and distribution lists can be
    /// used here, but it only defines for the SME address, so that is all that
    /// this will handle.
    /// </summary>
    public class UnsuccessAddress
    {
        private byte _DestAddressTON;
        private byte _DestAddressNPI;
        private string _DestAddress;
        private uint _ErrorStatusCode;

        /// <summary>
        /// Type of number for destination SME.
        /// </summary>
        public byte DestAddressTON
        {
            get
            {
                return _DestAddressTON;
            }
        }

        /// <summary>
        /// Numbering Plan Indicator for destination SME
        /// </summary>
        public byte DestAddressNPI
        {
            get
            {
                return _DestAddressNPI;
            }
        }

        /// <summary>
        /// Destination Address of destination SME
        /// </summary>
        public string DestAddress
        {
            get
            {
                return _DestAddress;
            }
        }

        /// <summary>
        /// Indicates the success or failure of the submit_multi request to this
        /// SME address.
        /// </summary>
        public UInt32 ErrorStatusCode
        {
            get
            {
                return _ErrorStatusCode;
            }
        }

        /// <summary>
        /// Creates an Unsuccess address.  This will trim down the address given to
        /// it for use in future operations.
        /// </summary>
        /// <param name="address">The bytes of the response.</param>
        public UnsuccessAddress(ref byte[] address)
        {
            _DestAddressTON = address[0];
            _DestAddressNPI = address[1];
            _DestAddress = SMPPResponse.GetStringFromBody(ref address, 2);
            _ErrorStatusCode = UnsignedNumConverter.SwapByteOrdering(
                                     BitConverter.ToUInt32(address, 0));
            byte[] remainder = SMPPResponse.ByteArrayCopy(
                                     address, 4, address.Length - 4);

            //and change the reference
            address = remainder;
        }

        /// <summary>
        /// Private constructor for cloning.
        /// </summary>
        private UnsuccessAddress()
        {
        }

        /// <summary>
        /// Clones this UnsuccessAddress.
        /// </summary>
        /// <returns>The cloned object.</returns>
        public object Clone()
        {
            UnsuccessAddress temp = new UnsuccessAddress();
            temp._DestAddressTON = _DestAddressTON;
            temp._DestAddressNPI = _DestAddressNPI;
            temp._DestAddress = _DestAddress;
            temp._ErrorStatusCode = _ErrorStatusCode;

            return temp;
        }
    }
}
